<template>
    <!-- components/dialog/index.wxml -->
    <view :class="'zan-dialog ' + (show ? 'zan-dialog--show' : '')">
        <!-- 如果想点击弹窗外不隐藏，取消bindtap点击事件即可 -->
        <view class="zan-dialog__mask" @tap="toggleDialog" />
        <view class="zan-dialog__container">
            <view class="dialog__body">
                <view class="dialog-title" v-if="label">修改{{ label }}</view>
                <input class="input" :type="inputType" :value="data" :placeholder="'请输入' + label" placeholder-class="placeholder-class" @input="handleInput" />
            </view>
            <view class="dialog__btns">
                <view class="btn cancel-btn" @tap.stop.prevent="cancel">取消</view>
                <view class="btn confirm-btn" @tap.stop.prevent="confirm">确定</view>
            </view>
        </view>
    </view>
</template>

<script>
// components/model/index.js
export default {
    data() {
        return {
            data: ''
        };
    },
    /**
     * 组件的属性列表
     */
    props: {
        show: {
            type: Boolean,
            default: false
        },
        label: {
            type: String,
            default: ''
        },
        inputType: {
            type: String,
            default: 'text'
        },
        initData: {
            type: String
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 输入信息
        handleInput: function (e) {
            this.setData({
                data: e.detail.value
            });
        },

        /**
         * 取消
         */
        cancel() {
            this.$emit('close');
        },

        /**
         * 确定
         */
        confirm() {
            let val = this.data;

            if (!val) {
                uni.showToast({
                    title: '请输入' + this.label,
                    icon: 'none'
                });
                return;
            }

            this.$emit('done', {
                detail: val
            });
        },

        toggleDialog() {
            console.log('占位：函数 toggleDialog 未声明');
        }
    },
    watch: {
        initData: {
            handler: function (newVal) {
                this.setData({
                    data: newVal
                });
            },

            immediate: true
        }
    }
};
</script>
<style lang="scss">
/* components/dialog/index.wxss */
.zan-dialog {
    display: none;
}

.zan-dialog.zan-dialog--show {
    display: block;
}

.zan-dialog__mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    background: rgba(0, 0, 0, 0.4); /*设置阴影半透明背景如： background: rgba(0, 0, 0, 0.4);*/
}

.zan-dialog__container {
    position: fixed;
    left: 91rpx;
    top: 50%;
    margin-top: -186rpx;
    z-index: 12;
    width: 568rpx;
    height: 374rpx;
    border-radius: 20rpx;
    font-size: 28rpx;
    text-align: center;
    border: 1px solid #bbb;
    background: #fff;
}
.zan-dialog__container .dialog__body {
    padding: 60rpx 40rpx;
}
.zan-dialog__container .dialog-title {
    font-size: 32rpx;
    color: $uni-text-color;
    margin-bottom: 48rpx;
}
.zan-dialog__container .input {
    height: 80rpx;
    line-height: 80rpx;
    border-radius: 10rpx;
    background-color: #f9f9f9;
    text-align: left;
    padding: 0 40rpx;
}
.zan-dialog__container .placeholder-class {
    color: $uni-text-color-grey;
}
.zan-dialog__container .dialog__btns {
    display: flex;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100rpx;
    border-top: 1px solid #eee;
}
.zan-dialog__container .dialog__btns .btn {
    width: 50%;
    font-size: 28rpx;
    color: $uni-text-color;
    line-height: 100rpx;
    text-align: center;
}
.zan-dialog__container .dialog__btns .btn + .btn {
    border-left: 1px solid #eee;
}
.zan-dialog__container .dialog__btns .confirm-btn {
    color: $uni-bg-color-main;
}
</style>
